package org.jeecg.modules.paycenter.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.ConvertUtil;
import org.jeecg.common.util.ZipUtils;
import org.jeecg.modules.paycenter.channel.pufa.PfBankService;
import org.jeecg.modules.paycenter.channel.pufa.PfBankUtil;
import org.jeecg.modules.wlhy.bill.entity.HyDriverBill;
import org.jeecg.modules.wlhy.bill.service.IHyDriverBillService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hydriver.entity.HyDriver;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hypfaccount.entity.HyPfAccount;
import org.jeecg.modules.wlhy.hypfaccount.service.IHyPfAccountService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 支付服务
 */
@RestController
@RequestMapping("/paycenter/pf")
@Slf4j
public class PFPayCenterController {

    @Autowired
    private IHyDriverService hyDriverService;

    @Autowired
    private IHyDriverBillService hyDriverBillService;

    @Autowired
    private IHyPfAccountService hyPfAccountService;

    @Autowired
    private IHyCarrierService hyCarrierService;


//   @Autowired
//   private PfBankService pfBankService;

    @Autowired
    private PfBankUtil pfBankUtil;

    @Autowired private UserUtil userUtil;

    @Value(value="${jeecg.path.bank}")
    public String bankPath;

    //region 司机

    @GetMapping(value = "/driver/bill/getTotal")
    public Result<?> driverBillGetTotal() {
        HyDriver driver = userUtil.getDriver();
        Map<String, String> totalAmtByDriver = hyDriverBillService.getTotalAmtByDriverId(driver.getId() , PfBankService.CHANNEL);
        return Result.OK(totalAmtByDriver);
    }
    @GetMapping(value = "/driver/bill/list")
    public Result<?> driverBillList(HyDriverBill hyDriverBill,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        HyDriver driver = userUtil.getDriver();
        QueryWrapper<HyDriverBill> queryWrapper = QueryGenerator.initQueryWrapper(hyDriverBill, req.getParameterMap());
        queryWrapper.eq("driver_id", driver.getId())
                .eq("resp_source","0").eq("pay_channel", PfBankService.CHANNEL).ne("bill_type","3");
        Page<HyDriverBill> page = new Page<HyDriverBill>(pageNo, pageSize);
        IPage<HyDriverBill> pageList = hyDriverBillService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping(value = "/driver/getAcccount")
    public Result<?> getDriverAcccount() {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        HyDriver driver = userUtil.getDriver();
        HyPfAccount hyPfAccount = hyPfAccountService.getDriverAccount(driver.getId());
        if(hyPfAccount == null) {
            return Result.error("未开通钱包！");
        }
        pfBankUtil.getChildAccountBalInfo(hyPfAccount);
        return Result.OK(hyPfAccount);
    }

    @AutoLog(value = "司机提现")
    @PostMapping(value = "/driver/tx")
    public Result<?> driverTx(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        HyDriver driver = userUtil.getDriver();
        return pfBankUtil.driverTx(driver , data);
    }

    @AutoLog(value = "创建司机虚拟子账户")
    @PostMapping(value = "/driver/createAcccount")
    public Result<?> createDriverAcccount() throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        HyDriver driver = userUtil.getDriver();
        HyPfAccount old = hyPfAccountService.getDriverAccount(driver.getId());
        if(old != null) {
            return Result.error("已开通，不能重复开通！");
        }
        return pfBankUtil.createDriverAcccount(driver);
    }

//    @AutoLog(value = "修改司机虚拟子账户")
//    @PostMapping(value = "/driver/editAcccount")
//    public Result<?> editDriverAcccount(@RequestBody JSONObject data) throws Exception {
//        HyConfig config = HyConfigUtils.getConfig();
//        if(!config.getPfIzUse()){
//            return Result.error("未开通服务");
//        }
//        HyDriver driver = userUtil.getDriver();
//        HyPfAccount old = hyPfAccountService.getDriverAccount(driver.getId());
//        if(old == null) {
//            return Result.error("未开通，请先开通子账号！");
//        }
//        //司机
//        String bscAcctNo = data.getString("bscAcctNo");
//        if(StringUtils.isBlank(bscAcctNo)){
//            return Result.error("银行卡不能为空");
//        }
//        old.setOpenBranchName("其他银行");
//        old.setOpenBrNo("9999");
//        old.setBscAcctNo(bscAcctNo);
//        old.setAcctName(old.getClientName());
//        return pfBankUtil.editAcccount(old);
//    }


    //endregion

    //region 货主
    @GetMapping(value = "/customer/getAcccount")
    public Result<?> getCustomerAcccount() {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }

        HyPfAccount hyPfAccount = hyPfAccountService.getCustomerAccount(customer.getId());
        if(hyPfAccount == null) {
            return Result.error("未开通钱包！");
        }
        hyPfAccount.setAcctNo(config.getPfAccNo());
        hyPfAccount.setKhh(config.getPfKhh());
        pfBankUtil.getChildAccountBalInfo(hyPfAccount);
        return Result.OK(hyPfAccount);
    }

    @AutoLog(value = "货主提现")
    @PostMapping(value = "/customer/tx")
    public Result<?> customerTx(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        String amt = data.getString("amt");
        if(!ConvertUtil.isMoney(amt)){
            return Result.error("提现金额不正确！");
        }

        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }

        HyPfAccount account = hyPfAccountService.getCustomerAccount(customer.getId());
        if(account == null) {
            return Result.error("钱包信息不存在！");
        }
        return pfBankUtil.customerTx(customer , account, amt);
    }

    @AutoLog(value = "创建货主子账户")
    @PostMapping(value = "/customer/createCustomerAcccount")
    public Result<?> createCustomerAcccount(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        String openBrNo = data.getString("openBrNo");
        String openBranchName = data.getString("openBranchName");
        String acctName = data.getString("acctName");
        String bscAcctNo = data.getString("bscAcctNo");
        if(StringUtils.isBlank(openBranchName)){
            return Result.error("开户行名称不能为空！");
        }
        if(StringUtils.isBlank(openBrNo)){
            return Result.error("开户行行号不能为空！");
        }
        if(StringUtils.isBlank(bscAcctNo)){
            return Result.error("账户账号不能为空！");
        }
        if(StringUtils.isBlank(acctName)){
            return Result.error("账户名称不能为空！");
        }

        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }

        String customerId = customer.getId();
        HyPfAccount old = hyPfAccountService.getCustomerAccount(customerId);
        if(old!=null) {
            return Result.error("已开通，不能重复开通！");
        }
        //货主
        HyPfAccount hyPfAccount = new HyPfAccount();
        hyPfAccount.setUid(customerId);
        hyPfAccount.setUtype("2");
        hyPfAccount.setClientName(customer.getCustomerName());
        hyPfAccount.setOpenBrNo(openBrNo);
        hyPfAccount.setOpenBranchName(openBranchName);
        hyPfAccount.setBscAcctNo(bscAcctNo);
        hyPfAccount.setAcctName(acctName);
        return pfBankUtil.createCustomerAcccount(hyPfAccount , customer);
    }

    @AutoLog(value = "修改货主签约信息")
    @PostMapping(value = "/customer/editCustomerAcccount")
    public Result<?> editCustomerAcccount(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        String openBrNo = data.getString("openBrNo");
        String openBranchName = data.getString("openBranchName");
        String acctName = data.getString("acctName");
        String bscAcctNo = data.getString("bscAcctNo");
        if(StringUtils.isBlank(openBranchName)){
            return Result.error("开户行名称不能为空！");
        }
        if(StringUtils.isBlank(openBrNo)){
            return Result.error("开户行行号不能为空！");
        }
        if(StringUtils.isBlank(bscAcctNo)){
            return Result.error("账户账号不能为空！");
        }
        if(StringUtils.isBlank(acctName)){
            return Result.error("账户名称不能为空！");
        }

        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }

//        if(!StringUtils.equals(acctName,customer.getCustomerName())){
//            return Result.error("客户名称和绑定的银行卡开户人名称必须一致！");
//        }

        String customerId = customer.getId();
        HyPfAccount hyPfAccount = hyPfAccountService.getCustomerAccount(customerId);
        if(hyPfAccount == null) {
            return Result.error("尚未开通钱包");
        }
        hyPfAccount.setOpenBrNo(openBrNo);
        hyPfAccount.setOpenBranchName(openBranchName);
        hyPfAccount.setBscAcctNo(bscAcctNo);
        hyPfAccount.setAcctName(acctName);

        return pfBankUtil.editAcccount(hyPfAccount);
    }


    //获取货主车队长钱包
    @GetMapping(value = "/customer/getCarrierAcccount")
    public Result<?> getCustomerCarrierAcccount(@RequestParam(name="carrierId",required=true) String carrierId) {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }
        HyCarrier carrier = hyCarrierService.getById(carrierId);
        if(carrier==null){
            return Result.error("车队长信息不存在");
        }
        if(!StringUtils.equals(carrier.getCustomerId(), customer.getId())){
            return Result.error("车队长信息错误");
        }

        HyPfAccount hyPfAccount = hyPfAccountService.getCarrierAccount(carrierId);
        if(hyPfAccount == null) {
            return Result.error("未开通钱包！");
        }

        hyPfAccount.setAcctNo(config.getPfAccNo());
        hyPfAccount.setKhh(config.getPfKhh());
        pfBankUtil.getChildAccountBalInfo(hyPfAccount);
        return Result.OK(hyPfAccount);
    }

    @AutoLog(value = "创建货主车队长子账号")
    @PostMapping(value = "/customer/createCarrierAcccount")
    public Result<?> createCustomerCarrierAcccount(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        String carrierId = data.getString("carrierId");
        if(StringUtils.isBlank(carrierId)){
            return Result.error("车队长信息不能为空！");
        }

        HyPfAccount old = hyPfAccountService.getCarrierAccount(carrierId);
        if(old != null) {
            return Result.error("已开通，不能重复开通！");
        }

        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }

        HyCarrier carrier = hyCarrierService.getById(carrierId);
        if(carrier==null){
            return Result.error("车队长信息不存在");
        }
        if(!StringUtils.equals(carrier.getCustomerId(), customer.getId())){
            return Result.error("车队长信息错误");
        }

        String idcard = carrier.getIdcard();
        if(StringUtils.isBlank(idcard)){
            return Result.error("开户人身份证号不能为空！");
        }

        String acctName = carrier.getBankName();
        String bscAcctNo = carrier.getBankNum();
        if(StringUtils.isBlank(bscAcctNo)){
            return Result.error("银行账号不能为空！");
        }
        if(StringUtils.isBlank(acctName)){
            return Result.error("银行账户名称不能为空！");
        }

        //车队长
        HyPfAccount hyPfAccount = new HyPfAccount();
        hyPfAccount.setUid(carrierId);
        hyPfAccount.setUtype("5");
        hyPfAccount.setClientName(acctName);
        hyPfAccount.setBscAcctNo(bscAcctNo);
        hyPfAccount.setAcctName(acctName);
        hyPfAccount.setOpenBranchName("其他银行");
        hyPfAccount.setOpenBrNo("9999");

        hyPfAccount.setCtfId(idcard);
        hyPfAccount.setCtfType("1");
        return pfBankUtil.createAcccount(hyPfAccount);

    }

    @AutoLog(value = "修改货主车队长签约信息")
    @PostMapping(value = "/customer/editCarrierAcccount")
    public Result<?> editCustomerCarrierAcccount(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }

        String acctName = data.getString("acctName");
        String bscAcctNo = data.getString("bscAcctNo");
        if(StringUtils.isBlank(bscAcctNo)){
            return Result.error("账户账号不能为空！");
        }
        if(StringUtils.isBlank(acctName)){
            return Result.error("账户名称不能为空！");
        }

        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }

        String carrierId = data.getString("carrierId");
        if(StringUtils.isBlank(carrierId)){
            return Result.error("车队长信息不能为空！");
        }

        HyCarrier carrier = hyCarrierService.getById(carrierId);
        if(carrier==null){
            return Result.error("车队长信息不存在");
        }

        if(!StringUtils.equals(carrier.getCustomerId(), customer.getId())){
            return Result.error("车队长信息错误");
        }

        HyPfAccount hyPfAccount = hyPfAccountService.getCarrierAccount(carrierId);
        if(hyPfAccount == null) {
            return Result.error("尚未开通钱包");
        }
        hyPfAccount.setBscAcctNo(bscAcctNo);
        hyPfAccount.setAcctName(acctName);
        return pfBankUtil.editAcccount(hyPfAccount);

    }

    @AutoLog(value = "货主车队长提现")
    @PostMapping(value = "/customer/carrierTx")
    public Result<?> customerCarrierTx(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }
        String amt = data.getString("amt");
        if(!ConvertUtil.isMoney(amt)){
            return Result.error("提现金额不正确！");
        }

        String carrierId = data.getString("carrierId");
        if(StringUtils.isBlank(carrierId)){
            return Result.error("车队长信息不能为空！");
        }

        HyCustomer customer = userUtil.getCustomer();
        if(customer == null){
            return Result.error("货主信息不存在");
        }

        HyCarrier carrier = hyCarrierService.getById(carrierId);
        if(carrier==null){
            return Result.error("车队长信息不存在");
        }

        if(!StringUtils.equals(carrier.getCustomerId(), customer.getId())){
            return Result.error("车队长信息错误");
        }

        HyPfAccount account = hyPfAccountService.getCarrierAccount(carrierId);
        if(account == null) {
            return Result.error("钱包信息不存在！");
        }
        return pfBankUtil.carrierTx(carrier , account, amt);
    }

    //endregion

    //region base

    @ApiOperation(value="hy_pf_account-分页列表查询", notes="hy_pf_account-分页列表查询")
	@GetMapping(value = "/getTransInfosList")
	public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

        if(!userUtil.isSys()){
            return Result.error("无权限操作");
        }
        HyConfig config = HyConfigUtils.getConfig();
        if(config == null || !config.getPfIzUse()){
            return Result.error("未开通浦发服务");
        }

        JSONObject params = new JSONObject();
        String qryStrtDt = req.getParameter("qryStrtDt");
        String qryEndDtTm = req.getParameter("qryEndDtTm");
        params.put("qryStrtDt",qryStrtDt);
        params.put("qryEndDtTm",qryEndDtTm);
//        params.put("qryStrtDt","20220501");
//        params.put("qryEndDtTm","20220731");
        params.put("queryNum",pageSize); //最大为20,最小为
        params.put("startNum",pageNo);
        String customerId = req.getParameter("customerId");
        if(!StringUtils.isEmpty(customerId)){
            HyPfAccount customerAccount = hyPfAccountService.getCustomerAccount(customerId);
            if(customerAccount == null){
                return Result.error("货主钱包不存在");
            }
            params.put("childAcctNo", customerAccount.getChildAcctNo());
        }
//        params.put("tranTpDsc","01,02,03,04,05,06");//系统交易类型
        params.put("tranTpDsc","01");//01 客户入金 02-客户出金
        Result<?> ret = pfBankUtil.getTransInfosList(params);
        if(ret.isSuccess()){
            JSONObject data = (JSONObject) ret.getResult();
            String strTranNum = data.getString("tranNum");//总笔数
            long tranNum = 0;
            if(!StringUtils.isEmpty(strTranNum)){
                tranNum = data.getLong("tranNum");//总笔数
            }
            JSONArray tranDtlQry = data.getJSONArray("tranDtlQry");
            //处理已下载回单文件
            List<JSONObject> jsonObjects = tranDtlQry.toJavaList(JSONObject.class);
            for (JSONObject jsonObject : jsonObjects) {
                String ylkTranSeqNo = jsonObject.getString("ylkTranSeqNo");
                String filePath = "/pf/rj/" +ylkTranSeqNo + ".pdf";
                if(FileUtil.exist(bankPath + filePath)){
                    jsonObject.put("bankHd",filePath);
                }
            }
            Page<JSONObject> page = new Page<>(pageNo, pageSize);
            page.setTotal(tranNum);
            page.setRecords(jsonObjects);
            return Result.OK(page);
        }else {
            return ret;
        }
	}

    @RequestMapping(value = "/downloadCustomerRjHd")
    public Result<?> downloadCustomerRjHd(HttpServletResponse response ,
                                          @RequestParam(name="ylkTranSeqNo")String ylkTranSeqNo,
                                          @RequestParam(name="childAcctNo")String childAcctNo) throws Exception {
        if (StringUtils.isEmpty(ylkTranSeqNo)) {
            return Result.error("ylkTranSeqNo不能为空");
        }
        if (StringUtils.isEmpty(childAcctNo)) {
            return Result.error("childAcctNo不能为空");
        }
        String fileName = "/pf/rj/" + ylkTranSeqNo + ".pdf";
        if (!FileUtil.exist(bankPath + fileName)) {
            //货主钱包
            HyPfAccount customerAccount = hyPfAccountService.getOne(
                    new QueryWrapper<HyPfAccount>().eq("child_acct_no", childAcctNo)
            );
            if (customerAccount == null) {
                return Result.error("货主钱包信息不存在");
            }
            //交易席位号
            String bidsSnglFlgCd = customerAccount.getBidsSnglFlgCd();
//            String bidsSnglFlgCd = "22022061617144195149";

            Result<?> ret = pfBankUtil.downloadCustomerRjHd(ylkTranSeqNo, bidsSnglFlgCd);
            if (!ret.isSuccess()) {
                return ret;
            }
        }
        //下载文件列表
        String filePath = bankPath + fileName;
        String pdfFileName = ylkTranSeqNo + ".pdf";
        ZipUtils.downLoadFile(response, new File(filePath), pdfFileName, "application/pdf");
        return Result.OK();
    }

    @GetMapping(value = "/getByUId")
    public Result<?> getByUId(@RequestParam(name="uid",required=true) String uid,
                              @RequestParam(name="utype",required=true) String utype) {
        HyPfAccount hyPfAccount = hyPfAccountService.getByUidAndUtype(uid,utype);
        if(hyPfAccount==null) {
            return Result.OK();
        }
        HyConfig config = HyConfigUtils.getConfig();
        hyPfAccount.setAcctNo(config.getPfAccNo());
        hyPfAccount.setKhh(config.getPfKhh());
        pfBankUtil.getChildAccountBalInfo(hyPfAccount);
        return Result.OK(hyPfAccount);
    }
    @AutoLog(value = "创建子账户")
    @PostMapping(value = "/batchCreateDriverAcccount")
    public Result<?> batchCreateDriverAcccount(@RequestParam(name="ids",required=true) String ids) {
        List<String> driverIds = Arrays.asList(ids.split(","));
        List<HyDriver> hyDrivers = hyDriverService.listByIds(driverIds);
        List<String> errList = new ArrayList<>();
        for (HyDriver driver : hyDrivers) {
            try {
                Result ret = pfBankUtil.createDriverAcccount(driver);
                if(!ret.isSuccess()){
                    errList.add(driver.getDriverName());
                }
            }catch (Exception e){
                errList.add(driver.getDriverName());
            }
        }
        if(errList.size() > 0){
            return Result.error(String.join(",", errList) + ":等司机钱包开通失败!" );
        }else {
            return Result.OK("开通成功！");
        }
    }

    @AutoLog(value = "创建子账户")
    @PostMapping(value = "/createAcccount")
    public Result<?> createAcccount(@RequestBody HyPfAccount hyPfAccount) throws Exception {
        log.info("-----开始进入浦发银行创建账号");
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }

        String utype = hyPfAccount.getUtype();
        if(StringUtils.isBlank(utype)){
            return Result.error("用户类型不能为空！");
        }
        String uid = hyPfAccount.getUid();
        if(StringUtils.isBlank(uid)){
            return Result.error("用户id不能为空！");
        }
        if(!"2".equals(utype) && !"4".equals(utype) && !"5".equals(utype) && !"9".equals(utype)){
            return Result.error("用户类型错误！");
        }
        if(!StringUtils.equals(hyPfAccount.getAcctName(),hyPfAccount.getClientName())){
            return Result.error("客户名称和绑定的银行卡开户人名称必须一致！");
        }
        HyPfAccount old = hyPfAccountService.getByUidAndUtype(uid,utype);
        if(old!=null) {
            return Result.error("已开通，不能重复开通！");
        }
        //货主
        if("2".equals(utype)){
            return pfBankUtil.createCustomerAcccount(hyPfAccount);
        }else if("9".equals(utype)){
            log.info("-----开始公司进入浦发银行创建账号");
            return pfBankUtil.createCompanyAcccount(hyPfAccount);
        }else if("4".equals(utype) ){
            //司机
            String bscAcctNo = hyPfAccount.getBscAcctNo();
            if(!StringUtils.isBlank(bscAcctNo)){
                hyPfAccount.setOpenBranchName("其他银行");
                hyPfAccount.setOpenBrNo("9999");
                hyPfAccount.setBscAcctNo(bscAcctNo);
                hyPfAccount.setAcctName(hyPfAccount.getAcctName());
            }else {
                hyPfAccount.setBscAcctNo("");
                hyPfAccount.setAcctName("");
                hyPfAccount.setOpenBranchName("");
                hyPfAccount.setOpenBrNo("");
            }
            return pfBankUtil.createDriverAcccount(hyPfAccount);
        }else if("5".equals(utype)){
            //车队长
            if(StringUtils.isBlank(hyPfAccount.getBscAcctNo())){
                return Result.error("账户账号不能为空！");
            }
            if(StringUtils.isBlank(hyPfAccount.getAcctName())){
                return Result.error("账户名称不能为空！");
            }
            hyPfAccount.setOpenBranchName("其他银行");
            hyPfAccount.setOpenBrNo("9999");
            return pfBankUtil.createCarrierAcccount(hyPfAccount);
        }
        return Result.error("用户类型错误！");
    }

    @AutoLog(value = "修改签约信息")
    @PostMapping(value = "/editAcccount")
    public Result<?> editAcccount(@RequestBody HyPfAccount hyPfAccount) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }

        String utype = hyPfAccount.getUtype();
        if(StringUtils.isBlank(utype)){
            return Result.error("用户类型不能为空！");
        }
        String uid = hyPfAccount.getUid();
        if(StringUtils.isBlank(uid)){
            return Result.error("用户id不能为空！");
        }
        if(!"2".equals(utype) && !"4".equals(utype) && !"5".equals(utype) && !"9".equals(utype)){
            return Result.error("用户类型错误！");
        }


        HyPfAccount old = hyPfAccountService.getByUidAndUtype(uid,utype);
        if(old==null) {
            return Result.error("未开通，请先开通子账号！");
        }

        //货主
        if("2".equals(utype) || "9".equals(utype)){
            if(!StringUtils.equals(hyPfAccount.getAcctName(),hyPfAccount.getClientName())){
                return Result.error("客户名称和绑定的银行卡开户人名称必须一致！");
            }
            if(StringUtils.isBlank(hyPfAccount.getOpenBranchName())){
                return Result.error("开户行名称不能为空！");
            }
            if(StringUtils.isBlank(hyPfAccount.getOpenBrNo())){
                return Result.error("开户行行号不能为空！");
            }
            if(StringUtils.isBlank(hyPfAccount.getBscAcctNo())){
                return Result.error("账户账号不能为空！");
            }
            if(StringUtils.isBlank(hyPfAccount.getAcctName())){
                return Result.error("账户名称不能为空！");
            }
        }else if("4".equals(utype) ){
            //司机
            String bscAcctNo = hyPfAccount.getBscAcctNo();
            if(!StringUtils.isBlank(bscAcctNo)){
                hyPfAccount.setOpenBranchName("其他银行");
                hyPfAccount.setOpenBrNo("9999");
                hyPfAccount.setBscAcctNo(bscAcctNo);
                hyPfAccount.setAcctName(hyPfAccount.getAcctName());
            }else {
                hyPfAccount.setBscAcctNo("");
                hyPfAccount.setAcctName("");
                hyPfAccount.setOpenBranchName("");
                hyPfAccount.setOpenBrNo("");
            }
        }else if("5".equals(utype)){
            if(!StringUtils.equals(hyPfAccount.getAcctName(),hyPfAccount.getClientName())){
                return Result.error("客户名称和绑定的银行卡开户人名称必须一致！");
            }
            //车队长
            if(StringUtils.isBlank(hyPfAccount.getBscAcctNo())){
                return Result.error("账户账号不能为空！");
            }
            if(StringUtils.isBlank(hyPfAccount.getAcctName())){
                return Result.error("账户名称不能为空！");
            }
            hyPfAccount.setOpenBranchName("其他银行");
            hyPfAccount.setOpenBrNo("9999");
        }
        return pfBankUtil.editAcccount(hyPfAccount);
    }

    @AutoLog(value = "提现")
    @PostMapping(value = "/tx")
    public Result<?> tx(@RequestBody JSONObject data) throws Exception {
        HyConfig config = HyConfigUtils.getConfig();
        if(!config.getPfIzUse()){
            return Result.error("未开通服务");
        }

        String utype = data.getString("utype");
        if(!"5".equals(utype) && !"9".equals(utype)){
            return Result.error("无权限操作");
        }
        String amt = data.getString("amt");
        if(!ConvertUtil.isMoney(amt)){
            return Result.error("提现金额不正确！");
        }

        String uid = data.getString("uid");

        if("5".equals(utype)){
            HyCarrier carrier = hyCarrierService.getById(uid);
            if(carrier == null){
                return Result.error("车队长信息不存在！");
            }
            boolean sys = userUtil.isSys();
            if(!sys){
                HyCustomer customer = userUtil.getCustomer();
                if(customer == null){
                    return Result.error("货主信息不存在");
                }
                if(!StringUtils.equals(carrier.getCustomerId() , customer.getId())){
                    return Result.error("车队长信息错误");
                }
            }

            String aid = data.getString("aid");
            //车队长提现
            HyPfAccount account = hyPfAccountService.getByUidAndUtype(uid,utype);
            if(account == null) {
                return Result.error("钱包信息不存在！");
            }
            if(!StringUtils.equals(aid,account.getId())){
                return Result.error("钱包信息错误！");
            }
            return pfBankUtil.carrierTx(carrier ,account , amt);
        }else if("9".equals(utype)){
            //分公司提现
            if(!userUtil.isSys()){
                return Result.error("无权限操作！");
            }
            //分公司
            HyPfAccount account = hyPfAccountService.getByUidAndUtype(uid,utype);
            if(account == null) {
                return Result.error("钱包信息不存在！");
            }
            String aid = data.getString("aid");
            if(!StringUtils.equals(aid,account.getId())){
                return Result.error("钱包信息错误！");
            }
            return pfBankUtil.companyTx(account , amt);
        }
        return Result.error("无权限操作！");
    }
    //endregion

    //region

    //endregion
}
